package drone.basis.util ;

import java.math.BigInteger ;

public class CompressUtil
{

	public static void main (
			String [ ] args
	)
	{

		String efa1 = CompressUtil.expand ( "1111222233334444" ) ;
		String wfa1 = CompressUtil.expand ( "5555666677778888" ) ;
		System.out.println ( "EFA累计电量 编号" ) ;
		System.out.println ( efa1 ) ;
		System.out.println ( "WFA累计流量 编号" ) ;
		System.out.println ( wfa1 ) ;

		String eua1 = CompressUtil.encode ( efa1 ) ;
		String wua1 = CompressUtil.encode ( wfa1 ) ;
		System.out.println ( "EUA电量 编号" ) ;
		System.out.println ( eua1 ) ;
		System.out.println ( "WUA水量 编号" ) ;
		System.out.println ( wua1 ) ;

		String ewp1 = CompressUtil.shrink ( eua1 ) + CompressUtil.shrink ( wua1 ) ;
		System.out.println ( "吨水电耗编号" ) ;
		System.out.println ( ewp1 ) ;

		System.out.println ( "===========================" ) ;
		String ewp2 = "22223333444455556666777788889999" ;
		String eua2 = CompressUtil.expand ( ewp2.substring ( 0 , 16 ) ) ;
		String wua2 = CompressUtil.expand ( ewp2.substring ( 16 , 32 ) ) ;
		System.out.println ( "EWP吨水电耗编号" ) ;
		System.out.println ( ewp2 ) ;

		System.out.println ( "EUA电量 分割" ) ;
		System.out.println ( eua2 ) ;
		System.out.println ( "WUA电量 分割" ) ;
		System.out.println ( wua2 ) ;

		System.out.println ( "EFA累计电量 编号" ) ;
		System.out.println ( CompressUtil.decode ( eua2 )  ) ;
		System.out.println ( "WFA累计电量 编号" ) ;
		System.out.println ( CompressUtil.decode ( wua2 )  ) ;

		// for(int i=0;i<1;i++){
		// String s=StringUtil.random ( 16 );
		// System.out.println ( s ) ;
		// String b= expand ( s ) ;
		// System.out.println ( b ) ;
		// String c=encode(b);
		// System.out.println ( c ) ;
		// String f=decode ( c );
		// System.out.println ( f ) ;
		// String p=shrink ( f );
		// System.out.println ( p ) ;
		// }
		//
		// String s=StringUtil.random ( 16 );
		// System.out.println ( s ) ;
		// String b= expand ( s ) ;
		// System.out.println ( b ) ;
		// String c=encode(b);
		// System.out.println ( c ) ;
		// String f=decode ( c );
		// System.out.println ( f ) ;
		// String p=shrink ( f );
		// System.out.println ( p ) ;
	}

	// 扩充字符串
	public static String expand (
			String source
		)
	{
		StringBuffer sb = new StringBuffer ( ) ;
		for ( int i = 0 ; i < source.length ( ) ; i ++ )
		{
			String item = source.substring ( i , i + 1 ) ;
			String res = new BigInteger ( item , 16 ).add ( new BigInteger ( "1" ) ).toString ( 16 ) ;
			sb.append ( item ).append ( ( res.equals ( "10" ) ? "0" : res ) ) ;
		}
		return sb.toString ( ).toUpperCase ( ) ;
	}

	// 收缩字符串
	public static String shrink (
			String source
		)
	{
		StringBuffer sb = new StringBuffer ( ) ;
		for ( int i = 0 ; i < source.length ( ) ; i ++ )
		{
			sb.append ( source.substring ( i , i ++ + 1 ) ) ;
		}
		return sb.toString ( ).toUpperCase ( ) ;
	}

	// 编码字符串
	public static String encode (
			String source
		)
	{
		StringBuffer sb = new StringBuffer ( ) ;
		for ( int i = 0 ; i < source.length ( ) ; i ++ )
		{
			String item = source.substring ( i , i + 1 ) ;
			String res = new BigInteger ( item , 16 ).add ( new BigInteger ( "1" ) ).toString ( 16 ) ;
			sb.append ( ( res.equals ( "10" ) ? "0" : res ) ) ;
		}
		return sb.toString ( ).toUpperCase ( ) ;
	}

	// 解码字符串
	public static String decode (
			String source
		)
	{
		StringBuffer sb = new StringBuffer ( ) ;
		for ( int i = 0 ; i < source.length ( ) ; i ++ )
		{
			String item = source.substring ( i , i + 1 ) ;
			String res = new BigInteger ( item , 16 ).subtract ( new BigInteger ( "1" ) ).toString ( 16 ) ;
			sb.append ( ( res.equals ( "-1" ) ? "F" : res ) ) ;
		}
		return sb.toString ( ).toUpperCase ( ) ;
	}

}
